package com.booking.springrestful.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.booking.springrestful.domain.Consultationroom;
import com.booking.springrestful.exception.BusinessException;
import com.booking.springrestful.mapper.ConsultationroomMapper;
import com.booking.springrestful.resp.ResultCodeEnum;
import com.booking.springrestful.service.ConsultationroomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 13182
* @description 针对表【consultationroom(咨询室表)】的数据库操作Service实现
* @createDate 2024-11-07 21:31:56
*/
@Service
@Slf4j
public class ConsultationroomServiceImpl extends ServiceImpl<ConsultationroomMapper, Consultationroom>
    implements ConsultationroomService {


    @Resource
    private ConsultationroomMapper consultationroomMapper;

    @Override
    public Integer addRoom(Consultationroom room) {
        consultationroomMapper.insert(room);
        List<Consultationroom> list = consultationroomMapper.selectList(new QueryWrapper<Consultationroom>().orderByDesc("consultationRoomId"));
        return list.get(0).getConsultationRoomId();
    }

    @Override
    public void updateRoom(Consultationroom room) {
        consultationroomMapper.updateById(room);
    }

    @Override
    public void deleteRoom(Consultationroom room) {
        log.info(room.toString());
        if (room.getTimetotal()!=null){
            throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR,"存在预约时间段不可删除");
        }
        consultationroomMapper.delete(new QueryWrapper<Consultationroom>().eq("consultationRoomId",room.getConsultationRoomId()));
    }

    @Override
    public List<Consultationroom> getRoom() {
        List<Consultationroom> list = consultationroomMapper.selectList(null);
        return list;
    }

    @Override
    public void deleteAll(List<Consultationroom> list) {
        list.forEach(item->{
            if (item.getTimetotal()==null||item.getTimetotal()==0){
                consultationroomMapper.deleteById(item);
            }else {
                throw new BusinessException(ResultCodeEnum.INTERNAL_SERVER_ERROR,"存在预约时间段不可删除");
            }
        });
    }


}




